package cn.edu.dgut.Filter;

//import com.sun.javaws.progress.PreloaderDelegate;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Map;

@WebFilter( "/user.jsp")
public class loginFilter implements Filter {
    public void destroy() {
    }

    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
        resp.setContentType("application/json");resp.setCharacterEncoding("UTF-8");
        HttpServletRequest request = (HttpServletRequest) req;
        HttpServletResponse response= (HttpServletResponse) resp;
        String uri = request.getRequestURI();
        if(uri.contains("/login/dgut")||uri.contains("/logoutServlet")) {
            chain.doFilter(req, resp);
        }   //不处理登陆和退出请求，只处理user.jsp的请求3

        //获取session
        Map<String,String> isLogin = (Map<String, String>) request.getSession().getAttribute("Login");
        HttpSession session = request.getSession(true);
        String  message=null;
        if(isLogin!=null){
            String login="-online";
            resp.getWriter().println(login);
            message="online";
            session.setAttribute("Loginsign", message);
        }
        else{
            String login="-offline";
            resp.getWriter().println(login);
            message="offline";
            session.setAttribute("Loginsign", message);
        }

        chain.doFilter(req, resp);

    }

    public void init(FilterConfig config) throws ServletException {

    }

}
